﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using PropertyManagementSystemAPI.Common.QiNiuYun;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using System.Net.Http.Headers;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 上传图片到七牛云
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UploadImgToQINiuYunController : ControllerBase
    {
        private readonly QnySetting Qny;
        public UploadImgToQINiuYunController(IOptions<QnySetting> _Qny)
        {
            Qny = _Qny.Value;
        }

        /// <summary>
        /// 获取token
        /// </summary>
        /// <returns>返回token信息</returns>
        [HttpGet]
        public IActionResult getUploadQiniuToken()
        {
            Mac mac = new Mac(Qny.qiniuyunAK, Qny.qiniuyunSK);// AK SK使用
            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Qny.qiniuyunBucket;
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());//token生成
            var json = new
            {
                token = token 
            };
            return Ok(json);
        }
    }
}
